# -*- conding:UTF-8 -*-

'''
@ Project: p6_421
@ File: serializers.py
@ Author: 罗婷
@ Date：2022/5/3 9:30

'''
from rest_framework import serializers
from rest_framework.pagination import PageNumberPagination
from rest_framework.serializers import ModelSerializer

from .models import *


# 品牌
class Brand_Serializer(ModelSerializer):
    logo = serializers.CharField(label='品牌logo', allow_null=True, trim_whitespace=True)
    class Meta:
        model = Brand
        fields = '__all__'
        read_only_fields = ['id',]


# 频道组
class Group_Serializer(ModelSerializer):
    # 反序列化输入cate 关系字段
    cate = serializers.PrimaryKeyRelatedField(queryset=Cate.objects.all())
    # 外键字段显示汉字
    cate_name = serializers.SerializerMethodField(read_only=True)

    def get_cate_name(self,obj):
        return obj.cate.name

    class Meta:
        model = Group
        fields = '__all__'
        read_only_fields = ['id',]

# 商品种类
class Cate_Serializer(ModelSerializer):

    class Meta:
        model = Cate
        fields = '__all__'

# SKU
class Good_Serializer(ModelSerializer):
    cate = serializers.PrimaryKeyRelatedField(queryset=Cate.objects.filter(cid=None))
    spu = serializers.PrimaryKeyRelatedField(queryset=SPU.objects.all())

    cate_name = serializers.SerializerMethodField(read_only=True)
    def get_cate_name(self, obj):
        return obj.cate.name

    class Meta:
        model = Good
        fields = '__all__'
        read_only_fields = ['id', 'image_default', ]

# SPU
class SPU_Serializer(ModelSerializer):
    brand = serializers.PrimaryKeyRelatedField(queryset=Brand.objects.all())
    brand_name = serializers.SerializerMethodField(read_only=True)
    def get_brand_name(self, obj):
        return obj.brand.name
    class Meta:
        model = SPU
        fields = '__all__'
        read_only_fields = ['id', ]

# SPU规格的序列化器  SerializerMethodField序列化器方法字段
class SPUSpecification_Serializer(ModelSerializer):
    spu = serializers.PrimaryKeyRelatedField(queryset=SPU.objects.all())
    spu_name = serializers.SerializerMethodField(read_only=True)
    def get_spu_name(self, obj): #obj 当前序列化的SPU规格的对象
        return obj.spu.name
    class Meta:
        model = SPUSpecification    #当前对哪个模型进行序列化
        fields = '__all__'         #要序列化哪些字段
        read_only_fields = ['id', ]


# 规格选项
class SpecOption_Serializer(ModelSerializer):
    spec = serializers.PrimaryKeyRelatedField(queryset=SPUSpecification.objects.all())
    spec_name = serializers.SerializerMethodField(read_only=True)

    def get_spec_name(self, obj):
        return obj.spec.name

    class Meta:
        model = SpecOption
        fields = '__all__'
        read_only_fields = ['id',]



# 规格选项 SKUImage
class SKUImage_Serializer(ModelSerializer):
    sku = serializers.PrimaryKeyRelatedField(queryset=Good.objects.all())
    sku_name = serializers.SerializerMethodField(read_only=True)

    def get_sku_name(self, obj):
        return obj.sku.name

    class Meta:
        model = SKUImage
        fields = '__all__'
        read_only_fields = ['id',]
